You can use a layer control in leaflet to allow your users to interactively turn layers on and off. The documentation for the leaflet package has some explanation of layer controls here.

Required packages

This example uses the following packages

library(tidyverse)
library(sf)
library(leaflet)
library(here)

Sample map

Here is a historic map of Boston with polygon layers showing 9-inch (dark blue) and 36-inch (light blue) sea-level rise scenarios, along with black circles for the locations of bike share stations.

leaflet() %>%
  addTiles(urlTemplate = "https://allmaps.xyz/maps/ad0210f48b2636b9/{z}/{x}/{y}.png") %>%
  addPolygons(data = hi_tide_36_inch,
              weight = 1,
              fillColor = "cornflowerblue",
              fillOpacity = 0.9) %>%
  addPolygons(data = hi_tide_9_inch,
              weight = 1,
              fillColor = "darkblue",
              fillOpacity = 0.9) %>%
  addCircleMarkers(data = bikes,
                   color = "black",
                   radius = 2,
                   opacity = 1) %>%
  setView(lng = -71.052164, lat = 42.360081, zoom = 15)

Toggle between base groups

I can use a base group to allow the user to toggle between the 9-inch and 36-inch sea-level rise scenarios.

I need to define a group name for each layer I want to toggle among by setting the group = parameter in the addPolygons() function. The name of the group should be the name that you want to appear on the layer control (which basically functions as a legend).

Then, I need to list those as base groups in the addLayersControl() function. I also need to set options = layersControlOptions(collapsed = FALSE) so that the layer controls are initially visible.

leaflet() %>%
  addTiles(urlTemplate = "https://allmaps.xyz/maps/ad0210f48b2636b9/{z}/{x}/{y}.png") %>%
  addPolygons(data = hi_tide_36_inch,
              weight = 1,
              fillColor = "cornflowerblue",
              fillOpacity = 0.9,
              group = "36-inch sea level rise") %>%
  addPolygons(data = hi_tide_9_inch,
              weight = 1,
              fillColor = "darkblue",
              fillOpacity = 0.9,
              group = "9-inch sea level rise") %>%
  addCircleMarkers(data = bikes,
                   color = "black",
                   radius = 2,
                   opacity = 1) %>%
  setView(lng = -71.052164, lat = 42.360081, zoom = 15) %>%
  addLayersControl(baseGroups = c("9-inch sea level rise",
                                  "36-inch sea level rise"),
    options = layersControlOptions(collapsed = FALSE)) 

Turn overlay groups on and off

I can use overlay groups to allow the user to turn the layer of bike stations on and off.

Again, I need to name the group within the addCircleMarkers() function. Then I list the name of that group as an overlay group.

leaflet() %>%
  addTiles(urlTemplate = "https://allmaps.xyz/maps/ad0210f48b2636b9/{z}/{x}/{y}.png") %>%
  addPolygons(data = hi_tide_36_inch,
              weight = 1,
              fillColor = "cornflowerblue",
              fillOpacity = 0.9,
              group = "36-inch sea level rise") %>%
  addPolygons(data = hi_tide_9_inch,
              weight = 1,
              fillColor = "darkblue",
              fillOpacity = 0.9,
              group = "9-inch sea level rise") %>%
  addCircleMarkers(data = bikes,
                   color = "black",
                   radius = 2,
                   opacity = 1,
                   group = "Blue Bike Stations") %>%
  setView(lng = -71.052164, lat = 42.360081, zoom = 15) %>%
  addLayersControl(baseGroups = c("9-inch sea level rise",
                                  "36-inch sea level rise"),
                   overlayGroups = c("Blue Bike Stations"),
    options = layersControlOptions(collapsed = FALSE))